<?php
namespace app\admin\controller;
//use think\facade\Cache;
class Tenant extends BaseAdmin{
    public function lists(){
        return $this->fetch();
    }

    public function balance(){
        return $this->fetch('balance');
    }


    public function upload(){
        $this->Tool->upload_file($folder='/upload/sfz');
    }

    public function status(){
        $_POST['tenant_status']=='true'?$_POST['tenant_status'] = 0:$_POST['tenant_status'] = 1;
        $save = $this->db->table('tenant')->where(array('id'=>$_POST['id']))->update($_POST);
        if($save){
            exit(json_encode(array('code'=>0,'msg'=>'修改成功')));
        }else{
            exit(json_encode(array('code'=>1,'msg'=>'修改失败')));
        }
    }


    public function add(){
        if(input('get.id')){
            $data = $this->db->table('tenant')->where(array('tenant_id'=>$_GET['id']))->item();
            $data['sales'] = '';
            $data['house'] = '';
            $user = $this->db->table('admin_admins')->where(['status'=>0])->lists();
            foreach ($user as $k=>$v) {
                if ($v['gid']==4) {
                    $data['sales'] .= '<option value="'.$v['id'].'">'.$v['truename'].'</option>';
                }
            }
            $house = $this->get_house();
            $xq = $this->get_residential_quarters();
            foreach ($house as $k=>$v) {
                $data['house'] .= '<option value="'.$v['hr_id'].'">'.$v['hr_city'].$xq[$v['hr_rq_id']]['rq_name'].$v['hr_entrance'].'-'.$v['hr_house_number'].'</option>';
            }
            $this->assign('data',$data);
        }else{
            $data = [
                'sales_id' => '',
                'house_id' =>'',
                'room_id' =>'',
                'tenant_name' => '',
                'tenant_phone' =>'',
                'tenant_certificates' => '',
                'tenant_idfrontimg' => '',
                'tenant_id' => '',
                'tenant_age' => '',
                'tenant_sex' => '',
                'sales' => '',
                'house' => '',
            ];
            $user = $this->db->table('admin_admins')->where(['status'=>0])->lists();
            foreach ($user as $k=>$v) {
                if ($v['gid']==4) {
                    $data['sales'] .= '<option value="'.$v['id'].'">'.$v['truename'].'</option>';
                }
            }
            $house = $this->get_house();
            $xq = $this->get_residential_quarters();
            foreach ($house as $k=>$v) {
                $data['house'] .= '<option value="'.$v['hr_id'].'">'.$v['hr_city'].$xq[$v['hr_rq_id']]['rq_name'].$v['hr_entrance'].'-'.$v['hr_house_number'].'</option>';
            }
            $this->assign('data',$data);
        }
        return $this->fetch();
    }


    public function save(){
        //exit(json_encode(array('code'=>1,'msg'=>$_POST)));
        $_POST['tenant_uptime']=time();
        $name = ['house_id','room_id','sales_id','tenant_name','tenant_sex','tenant_age','tenant_phone','tenant_age','tenant_certificates',];
        foreach ($name as $k=>$v) {
            empty($_POST[$v])&&$this->Tool->Tips(1,$v.'参数错误');
        }
        if($_POST['id']){
            $id = $this->db->table('tenant')->where(['id'=>$_POST['id']])->update($_POST);
        }else{
            $_POST['tenant_addtime']=time();
            unset($_POST['id']);
            $user = [
                'ua_name'=>$_POST['tenant_name'],
                'ua_phone'=>$_POST['tenant_phone'],
                'ua_pwd'=>md5($_POST['tenant_addtime'].'123456'),
                'ua_type'=>3,
                'ua_status'=>1,
                'ua_update_time'=>$_POST['tenant_addtime'],
                'ua_create_time'=>$_POST['tenant_addtime'],
            ];
            $ua_id = $this->db->table('user_account')->insert($user);
            $_POST['ua_id'] = $ua_id;
            $id = $this->db->table('tenant')->insert($_POST);
        }
        if($id){
            $this->Tool->Tips(0,'保存成功');
        }
        $this->Tool->Tips(1,'保存失败');
    }

    public function check(){
        $limit=input("get.limit")?input("get.limit"):10;
        $page=input("get.page")?input("get.page"):1;
        $limit=intval($limit);
        $page=intval($page);
        $start=$limit*($page-1);
        $where =array();
        isset($_GET['tenant_phone'])&&$where[]=['tenant_phone','=',$_GET['tenant_phone']];
        isset($_GET['tenant_name'])&&$where[]=['tenant_name','=',$_GET['tenant_name']];
        $tenants = $this->db->table('tenant')->where($where)->limit($start)->order('tenant_id', 'desc')->pages($limit);
        //dump($tenants);die;
        $count = $tenants["total"];
        $data =array(
            'code'=>'0',
            'msg'=>'0',
            'count'=>$count,
            'data'=>array(),
        );
        $sales = $this->get_admin();
        $houses = $this->get_house();
        $room = $this->get_room();
        $xq = $this->get_residential_quarters();
        foreach ($sales as $k=>$v){
            $sales_name[$v['id']]=$v['truename'];
        }
        foreach ($houses as $k=>$v){
            $house_name[$v['hr_id']]=$v['hr_city'].$v['hr_bc_id'].$v['hr_rq_id'].$v['hr_entrance'].$v['hr_storey'].$v['hr_house_number'];
        }
        foreach ($room as $k=>$v){
            $v['room_num']!=0&&$room_name[$v['r_id']]=$v['room_num'];
        }
        foreach ($tenants['lists'] as $k => $v) {
            $tenant['tenant_id'] = $v['tenant_id'];
            $tenant['tenant_name'] = $v['tenant_name'];
            $tenant['tenant_phone'] = $v['tenant_phone'];
            isset($sales_name[$v['sales_id']]) && $tenant['sales_id'] = $sales_name[$v['sales_id']];
            isset($house_name[$v['house_id']]) && $tenant['house_id'] = $house_name[$v['house_id']];
            isset($room_name[$v['room_id']]) && $tenant['room_id'] = $room_name[$v['room_id']];
            !empty($v['tenant_addtime']) && $tenant['tenant_addtime'] = date("Y-m-d H:i:s", (int)$v['tenant_addtime']);
            $tenant['operation'] = '
                <button class="layui-btn layui-btn-xs" id="' . $v['tenant_id'] . '"  lay-submit lay-filter="editor">编辑</button>
                <button class="layui-btn layui-btn-danger layui-btn-xs" id="' . $v['tenant_id'] . '"  lay-submit lay-filter="del">删除</button>
';
            if ($v['tenant_status'] == 0) {
                $tenant['tenant_status'] = '
                    <input type="checkbox" id="' . $v['tenant_id'] . '" name="status" lay-filter="switch" checked lay-skin="switch" lay-text="启用|禁用">
';
            } else {
                $tenant['tenant_status'] = '                       
                    <input type="checkbox" id="' . $v['tenant_id'] . '" name="status" lay-filter="switch" lay-skin="switch" lay-text="启用|禁用">
';
            }
            //$tenant['tenant_sex']==1?$tenant['tenant_sex']='男':$tenant['tenant_sex']='女';
            //!empty($tenant['tenant_uptime'])&&$tenant['tenant_uptime'] = date("Y-m-d H:i:s",(int)$tenant['tenant_uptime']);

            $data['data'][] = $tenant;
        }
        exit(json_encode($data));
    }


    public function order_check(){
        $limit=input("get.limit")?input("get.limit"):10;
        $page=input("get.page")?input("get.page"):1;
        $limit=intval($limit);
        $page=intval($page);
        $start=$limit*($page-1);
        $where =array();
        isset($_GET['tenant_phone'])&&$where[]=['tenant_phone','=',$_GET['tenant_phone']];
        isset($_GET['tenant_name'])&&$where[]=['tenant_name','=',$_GET['tenant_name']];
        $ord = $this->db->table('tenant')
            ->where($where)
            ->leftJoin('ty_elec_order',['ty_tenant.room_id=ty_elec_order.r_id'])
            ->pages();
        $data =array(
            'code'=>'0',
            'msg'=>'0',
            'count'=>$ord["total"],
            'data'=>array(),
        );
        $sales = $this->get_admin();
        $houses = $this->get_house();
        $room = $this->get_room();
        $xq = $this->get_residential_quarters();
        foreach ($sales as $k=>$v){
            $sales_name[$v['id']]=$v['truename'];
        }
        foreach ($houses as $k=>$v){
            $house_name[$v['hr_id']]=$v['hr_city'].$v['hr_bc_id'].$v['hr_rq_id'].$v['hr_entrance'].$v['hr_storey'].$v['hr_house_number'];
        }
        foreach ($room as $k=>$v){
            $v['room_num']!=0&&$room_name[$v['r_id']]=$v['room_num'];
        }
        foreach ($ord['lists'] as $k => $v) {
            $tenant['o_id'] = $v['o_id'];
            $tenant['t_id'] = $v['t_id'];
            $tenant['public_usage'] = $v['public_usage'];
            $tenant['room_usage'] = $v['room_usage'];
            $tenant['checkin_num'] = $v['checkin_num'];
            $tenant['total'] = $v['total'];
            $tenant['status'] = $v['status'];
            $tenant['create_time'] = $v['create_time'];
            isset($house_name[$v['h_id']]) && $tenant['h_id'] = $house_name[$v['h_id']];
            isset($room_name[$v['r_id']]) && $tenant['r_id'] = $room_name[$v['r_id']];
            $v['status']=1?$tenant['status']='已支付':'未支付';
            $tenant['operation'] = '
                <button class="layui-btn layui-btn-xs" id="' . $v['tenant_id'] . '"  lay-submit lay-filter="editor">编辑</button>
                <button class="layui-btn layui-btn-danger layui-btn-xs" id="' . $v['tenant_id'] . '"  lay-submit lay-filter="del">删除</button>
';
            //$tenant['tenant_sex']==1?$tenant['tenant_sex']='男':$tenant['tenant_sex']='女';
            //!empty($tenant['tenant_uptime'])&&$tenant['tenant_uptime'] = date("Y-m-d H:i:s",(int)$tenant['tenant_uptime']);

            $data['data'][] = $tenant;
        }
        exit(json_encode($data));
    }





















    public function room_id(){
        $tenants = $this->db->table('room')->where($_POST)->lists();
        if($tenants){
            $html = '<option value="">请选择房间号码</option>';
            foreach ($tenants as $v =>$k) {
                $k['room_num']!=0&&$html .= '<option value="'.$k['r_id'].'">'.$k['room_num'].'</option>';
            }
            exit(json_encode(array('code'=>0,'msg'=>$html)));
        }
        exit(json_encode(array('code'=>1,'msg'=>'没有查询到房间，请添加房间')));
    }

    

}
